package com.imedway.ihe.dtz.xds.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLAssociation30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLClassification30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLExtrinsicObject30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLQueryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryPackage30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationType;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.LocalizedString;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Version;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindFoldersQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAllQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsAndAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFolderAndContentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFoldersForDocumentQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.AssociationType1;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ClassificationType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ExtrinsicObjectType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryPackageType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dd.vo.GetAllVO;
import com.imedway.ihe.dd.vo.GetAssociationsVO;
import com.imedway.ihe.dd.vo.GetDocumentsAndAssociationsVO;
import com.imedway.ihe.dd.vo.GetFolderAndContentsVO;
import com.imedway.ihe.dd.vo.GetFoldersForDocumentVO;
import com.imedway.ihe.dd.vo.GetSubmissionSetsVO;
import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.entity.XdsAssociation;
import com.imedway.ihe.dtz.xds.entity.XdsClassification;
import com.imedway.ihe.dtz.xds.entity.XdsDescription;
import com.imedway.ihe.dtz.xds.entity.XdsExternalidentifier;
import com.imedway.ihe.dtz.xds.entity.XdsExtrinsicobject;
import com.imedway.ihe.dtz.xds.entity.XdsName;
import com.imedway.ihe.dtz.xds.entity.XdsRegistrypackage;
import com.imedway.ihe.dtz.xds.entity.XdsSlot;
import com.imedway.ihe.dtz.xds.util.BuildVoUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class Iti18Dao{
	
	@Autowired
	private BaseDao baseDao;
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public List<String> FIND_DOCUMENTS(FindDocumentsQuery findDocumentsQuery) {
							
		FindDocumentsVO vo = BuildVoUtil.buildFindDocumentsVO(findDocumentsQuery);	
		return baseDao.findExtrinsicobject(vo);
		
	}
	
	public List<String> FIND_SUBMISSION_SETS(FindSubmissionSetsQuery findDocumentsQuery) {
		FindSubmissionSetsVO vo = BuildVoUtil.buildFindSubmissionSetsVO(findDocumentsQuery);
		return baseDao.FIND_SUBMISSION_SETS(vo);
		
	}
	public List<String>  FIND_FOLDERS(FindFoldersQuery findDocumentsQuery) {
		FindFoldersVO vo = BuildVoUtil.buildFindFoldersVO(findDocumentsQuery);
		return baseDao.FIND_FOLDERS(vo);
		
	}
	public List<EbXMLAssociation> GET_ASSOCIATIONS(GetAssociationsQuery findDocumentsQuery) {
		GetAssociationsVO vo = BuildVoUtil.buildGetAssociationsVO(findDocumentsQuery);
		List<EbXMLAssociation> r =new ArrayList<EbXMLAssociation>();
		List<XdsAssociation> xdsAssociationList = baseDao.GET_ASSOCIATIONS(vo);
		for(XdsAssociation ox:xdsAssociationList) {
			EbXMLAssociation30 eb = new EbXMLAssociation30(new AssociationType1(), new EbXMLObjectLibrary());
			eb.setId(ox.getId());
			eb.setSource(ox.getSourceObject());
			eb.setTarget(ox.getTargetObject());
			eb.setStatus(AvailabilityStatus.valueOf(ox.getStatus()));
			eb.setAssociationType(AssociationType.valueOfOpcode30(ox.getAssociationType()));
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(ox.getId());
			List<XdsSlot> xdsSlotList=baseDao.GET_slot(xdsSlot);
			for(XdsSlot xds:xdsSlotList) {
				eb.addSlot(xds.getName_(), xds.getValue());					
			}
			r.add(eb);
		}		
		return r;		
	}
	
	
	public void GET_DOCUMENTS_AND_ASSOCIATIONS(EbXMLQueryResponse30 ebXMLQueryResponse30,GetDocumentsAndAssociationsQuery findDocumentsQuery) {
		GetDocumentsAndAssociationsVO vo = BuildVoUtil.buildGetDocumentsAndAssociationsVO(findDocumentsQuery);
		List<XdsExtrinsicobject> xdsExtrinsicobjectList= baseDao.GET_Extrinsicobject(vo);
		for(XdsExtrinsicobject xex:xdsExtrinsicobjectList) {
			   EbXMLExtrinsicObject30 ebXMLExtrinsicObject30 = new EbXMLExtrinsicObject30(new ExtrinsicObjectType(),
						new EbXMLObjectLibrary());
				ebXMLExtrinsicObject30.setMimeType(xex.getMimeType());
				ebXMLExtrinsicObject30.setId(xex.getId());
				ebXMLExtrinsicObject30.setStatus(AvailabilityStatus.valueOfOpcode(xex.getStatus()));
				ebXMLExtrinsicObject30.setObjectType(xex.getObjectType());
				ebXMLExtrinsicObject30.setHome(xex.getHome());
				ebXMLExtrinsicObject30.setLid(xex.getLid());
				
				XdsDescription xdsDescription = new XdsDescription();
				xdsDescription.setParent(xex.getId());
				GEN_XdsDescription(ebXMLExtrinsicObject30, xdsDescription);
										
				XdsName xdsName = new XdsName();
				xdsName.setParent(xex.getId());
				GEN_XdsName(ebXMLExtrinsicObject30, xdsName);
								
				ebXMLExtrinsicObject30.setVersionInfo(new Version(xex.getVersionName()));
				
				XdsSlot xdsSlot = new XdsSlot();
				xdsSlot.setParent(xex.getId());
				GEN_XdsSlot(ebXMLExtrinsicObject30, xdsSlot);
				
				XdsClassification xdsClassification =new XdsClassification();
				xdsClassification.setClassifiedObject(xex.getId());
				GEN_XdsClassification(ebXMLExtrinsicObject30,xdsClassification);
				
				XdsExternalidentifier xdsExternalidentifier =new XdsExternalidentifier();
				xdsExternalidentifier.setRegistryObject(xex.getId());				
				GEN_XdsExternalidentifier(ebXMLExtrinsicObject30,xdsExternalidentifier);
				
			    ebXMLQueryResponse30.addExtrinsicObject(ebXMLExtrinsicObject30);

			    XdsAssociation xdsAssociation1 =new XdsAssociation();
			    xdsAssociation1.setSourceObject(xex.getId());
			    GEN_XdsAssociation(ebXMLQueryResponse30, xdsAssociation1); 
			    
			    XdsAssociation xdsAssociation2 =new XdsAssociation();
			    xdsAssociation2.setTargetObject(xex.getId());
			    GEN_XdsAssociation(ebXMLQueryResponse30, xdsAssociation2); 

		}		
		
	}
	public List<EbXMLRegistryPackage> GET_SUBMISSION_SETS(GetSubmissionSetsQuery findDocumentsQuery) {
		GetSubmissionSetsVO vo = BuildVoUtil.buildGetSubmissionSetsVO(findDocumentsQuery);
		List<EbXMLRegistryPackage> r =new ArrayList<EbXMLRegistryPackage>();
		List<XdsRegistrypackage> xdsList = baseDao.GET_SUBMISSION_SETS(vo);
		for(XdsRegistrypackage ox:xdsList) {
			EbXMLRegistryPackage30 eb = new EbXMLRegistryPackage30(new RegistryPackageType(), new EbXMLObjectLibrary());
			eb.setId(ox.getId());
			eb.setLid(ox.getLid());
			eb.setObjectType(ox.getObjectType());
			eb.setStatus(AvailabilityStatus.valueOf(ox.getStatus()));
			eb.setHome(ox.getHome());
			
			XdsDescription xdsDescription = new XdsDescription();
			xdsDescription.setParent(ox.getId());
			GEN_XdsDescription(eb, xdsDescription);
									
			XdsName xdsName = new XdsName();
			xdsName.setParent(ox.getId());
			GEN_XdsName(eb, xdsName);
							
			eb.setVersionInfo(new Version(ox.getVersionName()));
			
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(ox.getId());
			GEN_XdsSlot(eb, xdsSlot);
			
			XdsClassification xdsClassification =new XdsClassification();
			xdsClassification.setClassifiedObject(ox.getId());
			GEN_XdsClassification(eb,xdsClassification);
			
			XdsExternalidentifier xdsExternalidentifier =new XdsExternalidentifier();
			xdsExternalidentifier.setRegistryObject(ox.getId());				
			GEN_XdsExternalidentifier(eb,xdsExternalidentifier);
						
			eb.setVersionInfo(new Version(ox.getVersionName()));
 
			r.add(eb);
	
		}		
		return r;	
		
	}
	public void GET_FOLDER_AND_CONTENTS(EbXMLQueryResponse30 ebXMLQueryResponse30,GetFolderAndContentsQuery findDocumentsQuery) {
		GetFolderAndContentsVO vo = BuildVoUtil.buildGetFolderAndContentsVO(findDocumentsQuery);
		XdsRegistrypackage registrypackage = new XdsRegistrypackage();
		registrypackage.setId(vo.get$XDSFolderEntryUUID());
		registrypackage.setLid(vo.get$XDSFolderUniqueId());
		registrypackage.setHome(vo.get$homeCommunityId());
		List<XdsRegistrypackage> xdsRegistrypackageList = baseDao.GET_registrypackage(registrypackage);
		for(XdsRegistrypackage ox:xdsRegistrypackageList) {
			
			EbXMLRegistryPackage30 eb = new EbXMLRegistryPackage30(new RegistryPackageType(), new EbXMLObjectLibrary());
			eb.setId(ox.getId());
			eb.setLid(ox.getLid());
			eb.setObjectType(ox.getObjectType());
			eb.setStatus(AvailabilityStatus.valueOf(ox.getStatus()));
			eb.setHome(ox.getHome());
			
			XdsDescription xdsDescription = new XdsDescription();
			xdsDescription.setParent(ox.getId());
			GEN_XdsDescription(eb, xdsDescription);
									
			XdsName xdsName = new XdsName();
			xdsName.setParent(ox.getId());
			GEN_XdsName(eb, xdsName);
							
			eb.setVersionInfo(new Version(ox.getVersionName()));
			
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(ox.getId());
			GEN_XdsSlot(eb, xdsSlot);
			
			XdsClassification xdsClassification =new XdsClassification();
			xdsClassification.setClassifiedObject(ox.getId());
			GEN_XdsClassification(eb,xdsClassification);
			
			XdsExternalidentifier xdsExternalidentifier =new XdsExternalidentifier();
			xdsExternalidentifier.setRegistryObject(ox.getId());				
			GEN_XdsExternalidentifier(eb,xdsExternalidentifier);
						
			eb.setVersionInfo(new Version(ox.getVersionName()));
 	    		  
			ebXMLQueryResponse30.addRegistryPackage(eb);
											
			
		}
		
		
	}
	public void GET_FOLDERS_FOR_DOCUMENT(EbXMLQueryResponse30 ebXMLQueryResponse30,GetFoldersForDocumentQuery findDocumentsQuery) {
		GetFoldersForDocumentVO vo = BuildVoUtil.buildGetFoldersForDocumentVO(findDocumentsQuery);
				
		XdsRegistrypackage registrypackage = new XdsRegistrypackage();
		registrypackage.setLid(vo.get$homeCommunityId());
		List<XdsRegistrypackage> xdsRegistrypackageList = baseDao.GET_registrypackage(registrypackage);
		for(XdsRegistrypackage ox:xdsRegistrypackageList) {
			
			EbXMLRegistryPackage30 eb = new EbXMLRegistryPackage30(new RegistryPackageType(), new EbXMLObjectLibrary());
			eb.setId(ox.getId());
			eb.setLid(ox.getLid());
			eb.setObjectType(ox.getObjectType());
			eb.setStatus(AvailabilityStatus.valueOf(ox.getStatus()));
			eb.setHome(ox.getHome());
			
			XdsDescription xdsDescription = new XdsDescription();
			xdsDescription.setParent(ox.getId());
			GEN_XdsDescription(eb, xdsDescription);
									
			XdsName xdsName = new XdsName();
			xdsName.setParent(ox.getId());
			GEN_XdsName(eb, xdsName);
							
			eb.setVersionInfo(new Version(ox.getVersionName()));
			
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(ox.getId());
			GEN_XdsSlot(eb, xdsSlot);
			
			XdsClassification xdsClassification =new XdsClassification();
			xdsClassification.setClassifiedObject(ox.getId());
			GEN_XdsClassification(eb,xdsClassification);
			
			XdsExternalidentifier xdsExternalidentifier =new XdsExternalidentifier();
			xdsExternalidentifier.setRegistryObject(ox.getId());				
			GEN_XdsExternalidentifier(eb,xdsExternalidentifier);
						
			eb.setVersionInfo(new Version(ox.getVersionName()));
 	    		  
			ebXMLQueryResponse30.addRegistryPackage(eb);
												
		}	
		
	}
	
	public void GET_ALL(EbXMLQueryResponse30 ebXMLQueryResponse30,GetAllQuery findDocumentsQuery) {
		GetAllVO vo = BuildVoUtil.buildGetAllVO(findDocumentsQuery);
		
		//查询提交集
		XdsExternalidentifier xdsExternalidentifier = new XdsExternalidentifier();
		xdsExternalidentifier.setStatus(vo.get$XDSSubmissionSetStatus());
		xdsExternalidentifier.setObjectType(EbXMLRegistryPackage30.REGISTRY_PACKAGE_OBJECT_TYPE);
		xdsExternalidentifier.setIdentificationScheme(SysConsts.SubmissionSetPatientIdExternalIdentifier);
		xdsExternalidentifier.setValue(vo.get$patientId());
		List<XdsExternalidentifier> xdsExternalidentifierList=baseDao.GET_externalidentifier(xdsExternalidentifier);
		
		for(XdsExternalidentifier xds:xdsExternalidentifierList) {
			XdsRegistrypackage registrypackage = new XdsRegistrypackage();
			registrypackage.setId(xds.getRegistryObject());
			EbXMLRegistryPackage30 eb = new EbXMLRegistryPackage30(new RegistryPackageType(), new EbXMLObjectLibrary());
			eb.setId(xds.getId());
			eb.setLid(xds.getLid());
			eb.setObjectType(xds.getObjectType());
			eb.setStatus(AvailabilityStatus.valueOf(xds.getStatus()));
			eb.setHome(xds.getHome());
			
			XdsDescription xdsDescription = new XdsDescription();
			xdsDescription.setParent(xds.getId());
			GEN_XdsDescription(eb, xdsDescription);
									
			XdsName xdsName = new XdsName();
			xdsName.setParent(xds.getId());
			GEN_XdsName(eb, xdsName);
							
			eb.setVersionInfo(new Version(xds.getVersionName()));
			
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(xds.getId());
			GEN_XdsSlot(eb, xdsSlot);
			
			XdsClassification xdsClassification =new XdsClassification();
			xdsClassification.setClassifiedObject(xds.getId());
			GEN_XdsClassification(eb,xdsClassification);
			
			XdsExternalidentifier xdsExternalidentifier1 =new XdsExternalidentifier();
			xdsExternalidentifier1.setRegistryObject(xds.getId());				
			GEN_XdsExternalidentifier(eb,xdsExternalidentifier1);
						
			eb.setVersionInfo(new Version(xds.getVersionName()));
 	    		  
			ebXMLQueryResponse30.addRegistryPackage(eb);
			
		}

		//查询文件夹
		
		//查询文档
		
		//查询关联关系
						
		
	}
	
	
	public void GEN_XdsDescription(EbXMLRegistryObject ebXMLRegistryObject,XdsDescription xdsDescription) {
		List<XdsDescription> xdsDescriptionList = baseDao.GET_description(xdsDescription);
		for(XdsDescription xds:xdsDescriptionList) {
			LocalizedString localizedString = new LocalizedString();
			localizedString.setCharset(xds.getCharset());
			localizedString.setLang(xds.getLang());
			localizedString.setValue(xds.getValue());
			ebXMLRegistryObject.setDescription(localizedString);
		}	
	}
	
	public void GEN_XdsName(EbXMLRegistryObject ebXMLRegistryObject,XdsName xdsName) {
		List<XdsName> xdsNameList = baseDao.GET_name(xdsName);
		for(XdsName xds:xdsNameList) {
			LocalizedString localizedString = new LocalizedString();
			localizedString.setCharset(xds.getCharset());
			localizedString.setLang(xds.getLang());
			localizedString.setValue(xds.getValue());
			ebXMLRegistryObject.setName(localizedString);

		}	
	}

	public void GEN_XdsSlot(EbXMLRegistryObject ebXMLRegistryObject,XdsSlot xdsSlot) {
		List<XdsSlot> xdsSlotList=baseDao.GET_slot(xdsSlot);
		for(XdsSlot xds:xdsSlotList) {
			ebXMLRegistryObject.addSlot(xds.getName_(), xds.getValue());					
		}
	}
	
	public void GEN_XdsClassification(EbXMLRegistryObject ebXMLRegistryObject,XdsClassification xdsClassification) {
		List<XdsClassification> xdsClassificationList=baseDao.GET_classification(xdsClassification);
		for(XdsClassification xds:xdsClassificationList) {
			EbXMLClassification30 classification =new EbXMLClassification30(new ClassificationType());
			classification.setClassificationNode(xds.getClassificationNode());
			classification.setClassificationScheme(xds.getClassificationScheme());
			classification.setClassifiedObject(xds.getClassifiedObject());
			classification.setNodeRepresentation(xds.getNodeRepresentation());
							
			ebXMLRegistryObject.addClassification(classification,xds.getClassificationScheme());
			
		}
	}
	
	public void GEN_XdsExternalidentifier(EbXMLRegistryObject ebXMLRegistryObject,XdsExternalidentifier xdsExternalidentifier) {
		List<XdsExternalidentifier> xdsExternalidentifierList =baseDao.GET_externalidentifier(xdsExternalidentifier) ;
		for(XdsExternalidentifier xds:xdsExternalidentifierList) {
			ebXMLRegistryObject.addExternalIdentifier(xds.getValue(), xds.getIdentificationScheme(), xds.getComment_());

		}
	}
	
	public void GEN_XdsAssociation(EbXMLQueryResponse30 ebXMLQueryResponse30,XdsAssociation xdsAssociation) {
	    List<XdsAssociation> xdsAssociation1List=baseDao.GET_associations(xdsAssociation); 
	    for(XdsAssociation xds:xdsAssociation1List) {
			EbXMLAssociation30 ebXMLAssociation30 = new EbXMLAssociation30(new AssociationType1(), new EbXMLObjectLibrary());
			ebXMLAssociation30.setId(xds.getId());
			ebXMLAssociation30.setSource(xds.getSourceObject());
			ebXMLAssociation30.setTarget(xds.getTargetObject());
			ebXMLAssociation30.setStatus(AvailabilityStatus.valueOfOpcode(xds.getStatus()));
			ebXMLAssociation30.setAssociationType(AssociationType.valueOfOpcode30(xds.getAssociationType()));
			ebXMLAssociation30.setLid(xds.getLid());
			ebXMLAssociation30.setHome(xds.getHome());
			XdsSlot xdsSlot = new XdsSlot();
			xdsSlot.setParent(xds.getId());
			GEN_XdsSlot(ebXMLAssociation30, xdsSlot);
			ebXMLQueryResponse30.addAssociation(ebXMLAssociation30);
	    }
	    
	}
}
